package android.bluebox.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluebox.R;
import android.bluebox.model.InitConfiguration;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InitQuestionView extends Activity {

	public static final String AES = "AES";

	EditText edtRemind;

	Button btnDone;
	Button btnBack;

	String strKey;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.initquestion);

		edtRemind = (EditText) findViewById(R.id.edtRemind);

		btnDone = (Button) findViewById(R.id.btnDone);
		btnDone.setOnClickListener(done);

		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(back);
	}

	private OnClickListener done = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			new Init().execute();
		}
	};

	private OnClickListener back = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			setResult(RESULT_CANCELED);
			finish();
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if(resultCode == RESULT_OK) //all done
		{
			this.setResult(RESULT_OK);
			this.finish();
		}
	}
	
	public class Init extends AsyncTask<Void, Void, Void> {

		private final ProgressDialog dialog = new ProgressDialog(InitQuestionView.this);
		boolean successful = false;
		
		protected void onPreExecute() {
			this.dialog.setTitle("Init system");
			this.dialog.setMessage("Please wait...");
			this.dialog.show();
		}
		
		@Override
		protected Void doInBackground(Void... arg0) {
			/**Get data from InitQuestionView
			 * pwd: password
			 * remind: Hint to remind password
			 */
			Bundle b = getIntent().getExtras();
			String pwd = b.getString("pwd");
			String remind = edtRemind.getText().toString();

			Intent intent = new Intent(InitQuestionView.this, InitConfiguration.class);

			/**Push password and remind question to InitConfiguration 
			 */
			intent.putExtra("pwd", pwd);
			intent.putExtra("remind", remind);

			startActivityForResult(intent, 0);
			return null;
		}
		
		protected void onProgressUpdate(final Void unused) {
		}
		
		protected void onPostExecute(final Void unused) {
			if (this.dialog.isShowing())
				this.dialog.dismiss();
			
			Toast.makeText(getBaseContext(), (successful)? "Login Successful" : "Login Unsuccessful", Toast.LENGTH_SHORT).show();
		}
	}
}
